<?php
	
	// Head
	require_once '../framework/header.inc.php';
	require_once '../framework/builder/book.class.php';
	
	//require_once('../framework/recaptchalib.php');
	//$publickey = "6Lfbu8ESAAAAAOxcHXrFJ2ZCn3_ijSDkHU3T0fri ";
	//$page->render("CAPTCHA",array("captcha" => recaptcha_get_html($publickey)));
	
	define('ID', $_GET['id']);
	
	
	// Set default page structure
	$extended['left'] = "navigation:sections,accordions:fromauthor:readmore:favorites";
	$extended['content:clearer'] = "path,bookparts,booktitle,comments,commentform";
	$page->constructor("default",$extended);
	
	
	// Set page scripts
	$style['jquery'] = array("jquery.tipsy.css");
	$page->setStyle($style);

	// Set page scripts
	$script['jquery'] = array("jquery.js", "jquery.accordion.js","jquery.tipsy.js","jquery.simplemodal.js");
	$extended = $page->loadFile("scripts/jquery/jquery.inc.js");
	$page->setScript($script,$extended);
	

	// Set book
	$book = new Book(ID);
	$book->setBook();
	
	$page->pageTitle($book->book['book_title']);
	$book->book['filename'] = $page->url['filename'];
		
		
	// if form is submited
	if (isset($_POST['comment'])) { 
		
		// Nese komenti nuk eshte shkruar 
		if ($_POST['comment'] == NULL)  $error['null'] = "comment";
		
		// If user isn't loged
		if (!isset($_SESSION['user_id']) && $_POST['name'] == NULL)  $error['null'] = "comment";
			
		
		// if is all ok
		if(empty($error)) {
			
			// Arkivo komentin
			$sql = mysql_query("INSERT INTO comment SET
								comment_category_id = '0',
								comment_book_id = '".ID."',
								comment_user_id = '".UID."',
								comment_content = '".$_POST['comment_content']."',
								comment_post_date = now() ");
			
			// Success
			if ($sql) $page->default['modal'] = $page->splash("addcomment");
			
		}
	}
	
	
	// Load all comments
	$sql = mysql_query("SELECT comment_user_id, comment_content, date_format(comment_post_date , '%e |%M| %Y  %H:%i:%s') as comment_post_date 
						FROM comment WHERE comment_category_id = '0' AND comment_book_id = '".ID."' ORDER BY comment_post_date DESC ");
	
	if (mysql_num_rows($sql) > 0) {
		
		while ($row = mysql_fetch_assoc($sql)) {
			
			$user = mysql_fetch_assoc(mysql_query("SELECT user_name, user_nickname, user_anonym FROM user WHERE user_id = '".$row['comment_user_id']."' "));
			$row['user_name'] = ($user['user_anonym']) ? $user['user_nickname'] : $user['user_name'];
			$row['comment_content'] = nl2br($row['comment_content']);
			$comments[] = $row;
		}
	}
		
		
	// Page render
	$page->render("CATEGORY",$page->category());
	$page->render("SECTIONS",$page->sections($book->book['book_section_id']));
	$page->render("FOOTMENU",$page->navigation("footmenu",null));
	$page->render("BOOKPARTS",$book->bookParts());
	$page->render("BOOKMENU",$page->navigation("bookmenu"));
	$page->render("FROMAUTHOR",$book->fromAuthor(ID));
	$page->render("READMORE",$book->readMore(ID));
	$page->render("FAVORITES",bookmarks(UID, SID));
	$page->render("COMMENTS",$comments);
	$page->render($book->book);
	$page->show();
	
?>